package com.wm.blog_admin.task;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.resp.IconExportResp;
import com.wm.blog_admin.query.TIconQuery;
import com.wm.blog_admin.service.TIconService;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TIconDO;
import com.wm.blog_common.result.Result;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * @author wm_yu
 * @date 2020-4-26 17:46
 */
@AllArgsConstructor
public class IconThreadTask implements Callable<List<IconExportResp>> {

    private Integer current;

    private Integer size;

    private TIconService tIconService;

    private TIconQuery tIconQuery;


    @Override
    public List<IconExportResp> call() throws Exception {
        QueryPage page = new QueryPage(current, size);
        Result<Page<TIconDO>> result = tIconService.page(page, tIconQuery);
        if(result.isSuccess() && !ObjectUtils.isEmpty(result.getData()) && !CollectionUtils.isEmpty(result.getData().getRecords())){
            return result
                    .getData()
                    .getRecords()
                    .stream()
                    .map(x -> {
                IconExportResp resp = new IconExportResp();
                BeanUtils.copyProperties(x, resp);
                return resp;
            })
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
